﻿using System;
using System.Collections.Generic;
using System.Linq;
using Guia3.Models;
using Guia3.Controllers;
using System.Web;
using Raven.Client;
using Raven.Client.Document;


namespace Guia3.Models
{
    public interface IVagaRepositorio
    {
        Vaga Load(string id);
        IEnumerable<Vaga> GetVagas();
        void Save(Vaga vaga);
        void Delete(string id);
    }

    public class VagaRepositorio : IVagaRepositorio
    {
        private IDocumentSession session;
        public VagaRepositorio(IDocumentSession session)
        {
            this.session = session;
        }

        public Vaga Load(string id)
        {
            return session.Load<Vaga>(id);
        }

        public IEnumerable<Vaga> GetVagas()
        {
            var vagas = session.LuceneQuery<Vaga>()
                .WaitForNonStaleResults()
                .ToArray();
            return vagas;
        }

        public void Save(Vaga vaga)
        {
            session.Store(vaga);
            session.SaveChanges();
        }

        public void Delete(string id)
        {
            var vaga = Load(id);
            session.Delete<Vaga>(vaga);
            session.SaveChanges();
        }
    }
}